package com.mutouren.web.sso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mutouren.common.entity.PageInfo;
import com.mutouren.common.entity.Result;
import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.common.web.BootStrapPage;
import com.mutouren.common.web.ParsePageInfo;
import com.mutouren.modules.sso.model.Register;
import com.mutouren.modules.sso.service.RegisterService;
import com.mutouren.web.WebUtils;

@Controller
@RequestMapping(value="/sso")
public class RegisterAction {
	
	@Resource
	private RegisterService registerService;	
	
	@RequestMapping(value="/registerManage.action")
	public String registerManage(HttpServletRequest request) {
		PageInfo pageInfo = ParsePageInfo.parse(request);
		
		List<Register> listRegister = registerService.select(new Register(ValidState.Invalid), pageInfo);
		request.setAttribute("listRegister", listRegister);
		
		request.setAttribute("mtrPage", BootStrapPage.buildHtmlPage(pageInfo));
		return "modules/sso/registerList";
	}
	
	@RequestMapping(value="/registerAdd.action")
	public String registerAdd(@ModelAttribute("register") Register register, 
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));		
		if (!WebUtils.isPost(request)) {		
			return "modules/sso/registerForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, register, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		Result result = registerService.register(register);
		if (result.getCode() == 0) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "应用别名已存在，请重新命名");
		}
	}
	
	@RequestMapping(value="/registerModify.action")
	public String registerModify(
			@ModelAttribute("register") Register register,
			@RequestParam(value="appId") int appId,
			ModelMap map,
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {
			register = registerService.get(appId);
			map.addAttribute("register", register);
			return "modules/sso/registerForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, register, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}		
		
		int result = registerService.modify(register);
		if (result == 1) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "编辑数据不存在");
		}				
	}	
	
	@RequestMapping(value="/changeState.action")
	public void changeState(@RequestParam(value="appId") int appId, String validState,
			HttpServletResponse response) throws IOException {
		Register paramRegister = new Register();
		paramRegister.setAppId(appId);
		paramRegister.setValidState(ValidState.valueOf(validState));
		
		if (registerService.setState(paramRegister) == 1) {
			response.getWriter().write("success");
		} else {
			response.getWriter().write("fail");
		}
	}
	
	@RequestMapping(value="/resetAppSecret.action")
	public void resetAppSecret(@RequestParam(value="appId") int appId, 
			HttpServletResponse response) throws IOException{
		
		registerService.resetAppSecret(appId);
		response.getWriter().write("success");
	}

	public boolean validate(HttpServletRequest request, Object model, List<String> errors ) {
		String actionName = (String) request.getAttribute("actionName");

		// register
		if (actionName.equals("registerAdd") || actionName.equals("registerModify")) {
			Register register = (Register) model;
			if (StringUtils.isBlank(register.getAppAlias())) {
				errors.add("应用别名: 不能为空");
			}
			if (StringUtils.isBlank(register.getAppName())) {
				errors.add("应用名称: 不能为空");
			}	
			if (StringUtils.isBlank(register.getAppUrl())) {
				errors.add("链接: 不能为空");
			}			
		}

		return errors.size() > 0;
	}
}
